package xmlcollabclient.staro;

import xmlcollabclient.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


/**
 *
 * @author Sasa
 */
public class ClientGUI {

    JFrame f; JPanel p, taskPanel;

    public ClientGUI() throws Exception{
        f = new JFrame();
        f.setSize(300, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // glavni panel
        //p = new JPanel();

        // toolbar
        JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);

        taskPanel = new JPanel();
        taskPanel.setLayout(new BoxLayout(taskPanel, BoxLayout.Y_AXIS));

        JButton refresh = new JButton();
        refresh.setIcon(new ImageIcon(getClass().getResource("/xmlcollabclient/icons/arrow-circle-225.png")));
        refresh.setText("Refresh");
        refresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              
               taskPanel.removeAll();

               try {
                  taskList(5);
               } catch(Exception e) {

               };
                
                taskPanel.validate();
                taskPanel.repaint();

            }
        });
        

        try {
                  taskList(5);
               } catch(Exception e) {

               };

        toolbar.add(refresh);
       // p.add(toolbar);
        //p.add(taskPanel);

        f.getContentPane().add(toolbar, BorderLayout.NORTH);
        f.getContentPane().add(taskPanel, BorderLayout.CENTER);
        //f.setUndecorated(true);
        //f.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
       // f.setContentPane(p);
        
        f.setVisible(true);

    }

    public void taskList(Integer start) throws Exception {

        Tasks tasks = new Tasks();

        List<Task> allTasks = tasks.getTasks(start);

        for(Integer i=0; i< allTasks.size(); i++) {

            final JCheckBox checkbox = allTasks.get(i).checkbox;

            taskPanel.add(checkbox);

            checkbox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    taskPanel.remove(checkbox);
                    taskPanel.revalidate();
                    taskPanel.repaint();
                }
            });

        }
        
    }

    public void complete() {

    }

    public static void main(String[] args) throws Exception {

        try {
                // Set cross-platform Java L&F (also called "Metal")
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (UnsupportedLookAndFeelException e) {
           // handle exception
        }
        catch (ClassNotFoundException e) {
           // handle exception
        }
        catch (InstantiationException e) {
           // handle exception
        }
        catch (IllegalAccessException e) {
           // handle exception
        }

        new ClientGUI(); //Create and show the GUI

    }
}

class Task {

    JCheckBox checkbox;
    Integer id;
    String assigment;
}

class Tasks {

    public  List<Task> getTasks(Integer start) throws Exception {

        List<Task> tasks = new ArrayList<Task>();

        // create 10 tasks
        for(Integer i = start; i<10; i++) {
            Task task = new Task();
            task.id = i;

            task.checkbox = new JCheckBox();
            task.assigment = "Test task " + i;
            task.checkbox.setText(task.assigment);

            tasks.add(task);
        }

        return tasks;
    }
}